﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using BySoft.Server.Business;
using OneSoft.Web.UI.WebControls;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Entity;

namespace BySoft.Web.Site.ComPage
{
    public class ClsCheck : BasePage
    {
        private Dictionary<string, string> CheckData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("MdStock", "MD0107");
            dc.Add("BsWorkorder", "MW0102");
            dc.Add("BsQuote", "MW0101");
            dc.Add("CgPurchase", "TG0103");
            dc.Add("CgApplypur", "TG0102");
            dc.Add("CgPurinstock", "SM0101");
            dc.Add("BsSalelist", "BS0101");
            dc.Add("BsTestvehicle", "BS0102");
            dc.Add("BsExternalaccept", "BS0103");
            dc.Add("BsRepairstatus", "BS0104");
            dc.Add("BsOutgood", "BS0105");
            dc.Add("BsRepairquote", "BS0106");
            dc.Add("BsRepaircollect", "BS0107");
            dc.Add("BsInneraccept", "BS0108");
            dc.Add("CgScrapparts", "SM0104");
            dc.Add("BmBom", "MD0122");
            dc.Add("CgPurreturn", "SM0102");
            dc.Add("CgOtherinstock", "SM0105");
            dc.Add("CgOtheroutstock", "SM0106");
            dc.Add("BmSendmatplan", "TG0101");
            dc.Add("BmSystemsend", "TG0104");
            dc.Add("CgProduceinstock", "SC0106");
            dc.Add("CgRequisition", "SC0101");
            dc.Add("BsHourrecord", "BS0109");
            dc.Add("BsWorkordercollect", "MW0103");
            dc.Add("BsCrossareaapply", "MW0107");
            dc.Add("BsRepairorder", "BS0110");
            dc.Add("BsSjworkorder", "TG0106");
            dc.Add("BsOvertime", "RS0101");

            return dc;
        }
        
        public static Dictionary<string, string> CheckDataPublic()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("MD0107", "MD_Stock");
            dc.Add("MW0102", "BS_WorkOrder");
            dc.Add("MW0101", "BS_Quote");
            dc.Add("TG0103", "CG_Purchase");
            dc.Add("TG0102", "CG_ApplyPur");
            dc.Add("SM0101", "CG_PurInStock");
            dc.Add("BS0101", "BS_SaleList");
            dc.Add("BS0102", "BS_TestVehicle");
            dc.Add("BS0103", "BS_ExternalAccept");
            dc.Add("BS0104", "BS_RepairStatus");
            dc.Add("BS0105", "BS_OutGood");
            dc.Add("BS0106", "BS_RepairQuote");
            dc.Add("BS0107", "BS_RepairCollect");
            dc.Add("BS0108", "BS_InnerAccept");
            dc.Add("SM0104", "Cg_Scrapparts");
            dc.Add("MD0109", "Bm_Bom");
            dc.Add("SM0102", "Cg_Purreturn");
            dc.Add("SM0105", "Cg_Otherinstock");
            dc.Add("SM0106", "Cg_Otheroutstock");
            dc.Add("TG0101", "Bm_Sendmatplan");
            dc.Add("TG0104", "Bm_Systemsend");
            dc.Add("SC0106", "Cg_Produceinstock");
            dc.Add("SC0101", "Cg_Requisition");
            dc.Add("BS0109", "BS_HourRecord");
            dc.Add("MW0103", "BS_WorkOrderCollect");
            dc.Add("MW0107", "BS_CrossAreaApply");
            dc.Add("BS0110", "BS_RepairOrder");
            dc.Add("TG0106", "BS_SJWorkOrder");
            dc.Add("RS0101", "BS_OverTime");
            return dc;
        }

        private string GetCheckData(string strKey)
        {
            Dictionary<string, string> dc = CheckData();
            if (!dc.ContainsKey(strKey))
            {
                throw new Exception("请联系系统管理员，设置审核参数！");                
            }
            return dc[strKey];
        }

        public string TblName
        {
            get
            {
                return m_strTbl;
            }
            set
            {
                m_strTbl = value;
                string[] strs = m_strTbl.Split('_');
                if (strs.Length != 2)
                {
                    throw new Exception("表名必须用[_]分隔");
                }
                m_SignName = strs[0][0] + strs[0].Substring(1).ToLower() + strs[1][0] + strs[1].Substring(1).ToLower();
            }
        }
        private string m_strTbl = string.Empty;
        private string m_SignName = string.Empty;
        private int m_iCheckCol = 4;

        public string GetModuleID(string strTblName)
        {
            TblName = strTblName;
            return GetCheckData(m_SignName);
        }

        public void VisibleCheckCol(string strTblName,SGridView grdResult)
        {
            string strUserID = CurrentUser.UserID;
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);
            bool b = ClsComm.IsExistAuditUser(strSign, strUserID);
            if (!b)
            {
                grdResult.Columns[grdResult.Columns.Count - m_iCheckCol].Visible = false;
                grdResult.Columns[grdResult.Columns.Count - 5].Visible = false;
            }
        }

        public bool IsAllCheck(string strTblName, string strMainKey)
        {
            string strUserID = CurrentUser.UserID;
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);

            if (ClsComm.IsAllUserCheck(strSign, strMainKey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void EnableCheckBtn(string strTblName, string strMainKey, ImageButton btnCheck)
        {
            string strUserID = CurrentUser.UserID;
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);
            
            if (ClsComm.IsAllUserCheck(strSign, strMainKey))
            {
                btnCheck.Visible = false;
            }
            else
            {
                btnCheck.Visible = true;
                if (ClsComm.IsUserHaveCheck(strSign, strMainKey, strUserID))
                {
                    btnCheck.ImageUrl = "../App_Themes/Common_CSS/images/uncheck.gif";
                    btnCheck.Enabled = false;
                }
                else
                {
                    btnCheck.ImageUrl = "../App_Themes/Common_CSS/images/check.gif";
                    btnCheck.Enabled = true;
                }
            }
        }

        public void VisibleLowVersion(string strTblName, string strMainKey, ImageButton btnCheck)
        {
            string strUserID = CurrentUser.UserID;
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);

            if (ClsComm.IsAllUserCheck(strSign, strMainKey))
            {
                btnCheck.Visible = false;
            }
            else
            {
                btnCheck.Visible = true;
                if (ClsComm.IsHighVersion(m_SignName, strMainKey))
                {
                    btnCheck.Visible = true;
                }
                else
                {
                    btnCheck.Visible = false;
                }
            }
        }

        //public bool CheckBill(string strTblName,string strMainKeyField, string strMainKey)
        //{
        //    string strUserID = CurrentUser.UserID;
        //    TblName = strTblName;
        //    string strSign = GetCheckData(m_SignName);

        //    if (ClsComm.IsAllUserCheck(strSign, strMainKey))
        //    {
        //        return false;
        //    }

        //    SysBillcheckrecordsEntity entRecord = new SysBillcheckrecordsEntity();
        //    entRecord.BillType = strSign;
        //    entRecord.BillID = strMainKey;
        //    entRecord.CheckUser = strUserID;
        //    entRecord.CheckDate = DateTime.Now;
        //    ClsComm.SaveCheckRecord(entRecord);

        //    if (ClsComm.IsAllUserCheck(strSign, strMainKey))
        //    {
        //        ClsComm.UpdateCheckStatus(strTblName, strMainKeyField, strMainKey);
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        public bool IsCheckBill(string strTblName)
        {
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);
            return ClsComm.IsCheckBill(strSign);
        }

        public void DelCheckRecord(string strTblName, string strID)
        {
            TblName = strTblName;
            string strSign = GetCheckData(m_SignName);
            ClsComm.DelCheckRecord(strSign, strID);
        }

        public void VisibleEdit(string strStatus, ImageButton ibtnEdi)
        {
            if (strStatus.Equals("2"))
            {
                ibtnEdi.Visible = false;
            }
            else
            {
                ibtnEdi.Visible = true;
            }
        }
    }

    public class ClsView : BasePage
    {
        public void VisibleAmount(string strTblName, string strUserID, SGridView grdResult, int[] visibleCol)
        {
            if (visibleCol.Length < 1 || strUserID.Equals("ByAdmin"))
            {
                return;
            }
            ClsCheck cls = new ClsCheck();
            string strSign = cls.GetModuleID(strTblName);

            bool b = ClsComm.IsViewBill(strSign, strUserID);
            if (!b)
            {
                for (int i = 0; i < visibleCol.Length; i++)
                {
                    grdResult.Columns[grdResult.Columns.Count - visibleCol[i]].Visible = false;
                }
            }
        }
    }
}